package com.gw.booking.controller.common;

import com.google.common.io.ByteStreams;
import com.gw.booking.common.MessageCode;
import com.gw.booking.common.ResultObject;
import com.gw.booking.common.Util;
import com.gw.booking.entity.Attachment;
import com.gw.booking.service.AttachmentService;
import config.Config;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;

@Api(tags={"共通-附件"})
@Controller
@RequestMapping("/commons/attachments")
public class CAttachmentController {
	@Autowired
	private AttachmentService attachmentService;

	@ApiOperation(value="下载附件")
	@RequestMapping(value = "download.do", method = {RequestMethod.GET})
	public void download(@ApiParam(value = "附件名称") @RequestParam String filename
			, @ApiParam(value = "保存文件名,导出的文件下载时必传") @RequestParam(required = false) String saveName
			, HttpServletResponse response)
			throws IllegalAccessException, InvocationTargetException, IllegalStateException, IOException {

		saveName = Util.encodeUriParam(saveName);

		if (!filename.startsWith("tmp/")) {
			Attachment attachment = attachmentService.getAttachment(filename);

			response.setContentType(attachment.getMime());
			response.setHeader("Content-Disposition"
					, "attachment;filename=" + URLEncoder.encode(attachment.getFilenameOriginal(), "UTF-8"));
			ByteStreams.copy(new FileInputStream(Config.attachFolder + attachment.getFilename())
					, response.getOutputStream());
		} else {
			String mime = Files.probeContentType(Paths.get(Config.attachFolder + filename));
			saveName = StringUtils.isEmpty(saveName) ? filename : saveName;
			response.setContentType(mime);
			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(saveName, "UTF-8"));
			ByteStreams.copy(new FileInputStream(Config.attachFolder + filename), response.getOutputStream());
		}
	}

	@ApiOperation(value="上传附件")
	@RequestMapping(value = "upload.do", method = {RequestMethod.POST})
	@ResponseBody
	public ResultObject<String> upload(@ApiParam(value = "附件") @RequestPart MultipartFile multipartFile)
			throws IllegalAccessException, InvocationTargetException, IllegalStateException, IOException {

		String filename = attachmentService.txUpload(multipartFile);

		return new ResultObject<>(MessageCode.CODE_SUCCESS, filename);
	}

	@ApiOperation(value="上传临时附件")
	@RequestMapping(value = "uploadTmp.do", method = {RequestMethod.POST})
	public void uploadTmp(HttpServletResponse response, @ApiParam(value = "附件") @RequestPart MultipartFile multipartFile)
			throws IllegalAccessException, InvocationTargetException, IllegalStateException, IOException {
		response.setContentType("text/html");
		String filename = attachmentService.txUploadTmp(multipartFile);

		response.getWriter().println(JSONObject.fromObject(new ResultObject<>(MessageCode.CODE_SUCCESS, filename)).toString());
	}
}
